//#############################################################################
//#############################################################################
//This source file was written by Mert Dogar. If you have some questions about 
//the source code, please do not hesitate to ask me.
//The code is under GPL.
//Email: mertdogar@gmail.com , http://code.google.com/p/electron-cds
//#############################################################################
//#############################################################################

#include "main.h"
#include "project.h"


Gtk::Window* property_window = 0;
void on_property_close();

/*
  //For the validated column:
  //You could also use a CellRendererSpin or a CellRendererProgress:
  Gtk::CellRendererText m_cellrenderer_validated;
  Gtk::TreeView::Column m_treeviewcolumn_validated;
  bool m_validate_retry;
  Glib::ustring m_invalid_text_for_retry;
*/

  
  //Tree model columns:
  class ModelProperty : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelProperty()
    { add(m_col_property); add(m_col_value);  add(m_col_visible); }

    Gtk::TreeModelColumn<Glib::ustring> m_col_property;
    Gtk::TreeModelColumn<Glib::ustring> m_col_value;
	Gtk::TreeModelColumn<bool> m_col_visible;

  };
  
  ModelProperty pcolumn;

  Gtk::TreeView* tree;

  //The Tree model:
  Glib::RefPtr<Gtk::ListStore> PropertyRef;



void show_property_window(Glib::RefPtr<Gnome::Glade::Xml> refXml, project &prj)
{
	
	Gtk::Button* btn;
	log("Loading property window");
	log("Gonna get button and property window pointers");
	refXml->get_widget("property_window",property_window);
	refXml->get_widget("property_window_close",btn);
	refXml->get_widget("property_window_treeview",tree);
	btn->signal_clicked().connect(sigc::ptr_fun(on_property_close));
	log("Signals connected");
	
	
	//Create the Tree model:
	PropertyRef = Gtk::ListStore::create(pcolumn);
	tree->set_model(PropertyRef);
	tree->set_reorderable();
	Gtk::TreeModel::Row row;
	log("Tree model defined");
	if(get_selected_index () >= 0)
	{	
	for(int index=0;index <	prj.get_component(get_selected_index ()).get_property_size(); index++)
	{
		//Fill the TreeView's model
		log("Attempting to append a row");
		row = *(PropertyRef->append());
		log("Adding component to list");
		row[pcolumn.m_col_property] = 	prj.get_component(get_selected_index ()).get_property_name(index);
		row[pcolumn.m_col_value] = prj.get_component(get_selected_index ()).get_property_value(index);	
		row[pcolumn.m_col_visible] = prj.get_component(get_selected_index ()).get_property_visible(index);	
	}
	}

	

	//Add the TreeView's view columns:
	tree->remove_all_columns();
	tree->append_column_editable("Property", pcolumn.m_col_property);
	tree->append_column_editable("Value", pcolumn.m_col_value);
	tree->append_column_editable("Visible", pcolumn.m_col_visible);
	log("Column names were set");

	/*
	//For this column, we create the CellRenderer ourselves, and connect our own
	//signal handlers, so that we can validate the data that the user enters, and
	//control how it is displayed.
	m_treeviewcolumn_validated.set_title("validated (<10)");
	m_treeviewcolumn_validated.pack_start(m_cellrenderer_validated);
	tree->append_column(m_treeviewcolumn_validated);

	//Tell the view column how to render the model values:
	m_treeviewcolumn_validated.set_cell_data_func(m_cellrenderer_validated,
			sigc::ptr_fun(treeviewcolumn_validated_on_cell_data) );

	//Make the CellRenderer editable, and handle its editing signals:
	#ifdef GLIBMM_PROPERTIES_ENABLED
		m_cellrenderer_validated.property_editable() = true;
	#else
		m_cellrenderer_validated.set_property("editable", true);
	#endif

	m_cellrenderer_validated.signal_editing_started().connect(
			sigc::ptr_fun(cellrenderer_validated_on_editing_started) );

	m_cellrenderer_validated.signal_edited().connect( sigc::ptr_fun(cellrenderer_validated_on_edited) );

	*/
	
	property_window->show();
	log("Show property window");
}

void on_property_close()
{
	
	property_window->hide();
	
}
